﻿using XiaoYuStockAnalysis.BusinessLogic.Business;
using XiaoYuStock.Core;
using XiaoYuStock.Model;
using XiaoYuStock.Model.Models;
using XiaoYuStock.Model.Request;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XiaoYuStock.Hosts
{
    public class UpdateHistoryService
    {

        private static ShareListDataBusiness shareBus = new ShareListDataBusiness();

        private static HistoryDataBusiness historyBus = new HistoryDataBusiness();
        /// <summary>
        /// 分析数据
        /// </summary>
        /// <param name="days"></param>
        /// <param name="guidePrice"></param>
        public static void UpdateHistoryOpt()
        {

            int totalRecord = 0;
            ShareRequest shareRequest = new ShareRequest();
            shareRequest.PageSize = 100;
            shareRequest.ShareStatus = (int)ShareState.Active;
            shareRequest.PageIndex = 1;
            int pageCount = 0;
            do
            {
                var result = shareBus.GetList(shareRequest,out totalRecord);
                if (pageCount == 0)
                {
                    pageCount = (totalRecord + shareRequest.PageSize - 1) / shareRequest.PageSize;
                }
                foreach (var item in result)
                {
                    LogHelper.InfoAsync("执行历史数据补充：" + item.ShareCode);
                    var history = GetHistoryData(item.ShareCode);
                    UpdateTradeRate(history);
                }
                shareRequest.PageIndex++;
                
            }
            while (shareRequest.PageIndex <= pageCount);



        }

        /// <summary>
        /// 获取历史数据
        /// </summary>
        /// <param name="shareCode"></param>
        /// <param name="days"></param>
        /// <returns></returns>
        private static IEnumerable<HistoryData> GetHistoryData(string shareCode)
        {
            HistoryRequest historyRequest = new HistoryRequest();
            historyRequest.ShareCode = shareCode;
            historyRequest.PageSize = 30;
            historyRequest.MaxRate = 0.01m;
            return historyBus.GetDataList(historyRequest);
        }

        private static void UpdateTradeRate(IEnumerable<HistoryData> data)
        {
            if (data != null && data.Any())
            {
                var currentData = data.OrderByDescending(s => s.TradeTime).ToList();
                var lastData = data.FirstOrDefault();

                HistoryData tempData=null;
                 
                for (var i = 1; i < currentData.Count; i++)
                {
                    tempData = currentData[i];
                    lastData.DiffPrice = lastData.CloseAmout - tempData.CloseAmout;
                    if (tempData.CloseAmout > 0)
                    {
                        lastData.TradeRate = Math.Round((decimal)(lastData.DiffPrice / tempData.CloseAmout) * 100, 2);
                    }
                    historyBus.SynData(lastData);
                    lastData = tempData;
                }
              
            }
        }
    }
}
